﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Slam.Configuration;
using Slam.SharePoint;
using SlamContext.Cache;
using Slam.Logging;

namespace Slam
{
	public class SlamContext
	{	
		private static Constants _constants;
		private static CacheManager _cache;
        private ILog _log;
        private SlamType _type;
        private SlamItem _item;

		private TypeContainer _typeContainer;
		private SlamSettings _settings;

        protected static Dictionary<string, SlamContext> AllTypeContexts;

		internal static CacheManager Cache
		{
			get
			{
				return _cache;
			}
		}

		public ILog Log
		{
			get
			{
                return _log;
			}
		}

		public static Constants Constants
		{
			get
			{
				return _constants;
			}
		}

		internal static SlamType Global
		{
			get
			{	
				return new SlamType(Constants.DefaultTypeName);
			}
		}

        public SlamItem Item
        {
            get
            {
                return _item;
            }
        }

        public SlamType Type
        {
            get
            {
                return _type;
            }
        }

		static SlamContext()
		{
			_cache = new CacheManager();
			_constants = new Constants();
			SlamContext.Cache.Add(Constants.Cache.SlamActionCollection, new Dictionary<string, SlamActionCollection>(), CacheScope.Thread);
			AllTypeContexts = new Dictionary<string, SlamContext>();
		}

        public SlamContext(ILog log, SlamSettings settings)
        {
            _log = log;
			_settings = settings;
			
        }

		//public SlamContext Get(SPList list)
		//{
		//	TypeContainer container = null;
		//	if (_typeContainers.ContainsKey(list.ID.ToString()))
		//	{
		//		container = _typeContainers[list.ID.ToString()];
		//	}
		//	else
		//	{
		//		container = new TypeContainer(list);
		//	}
		//	return container.Resolve<SlamContext>();
		//}

		public static SlamType Get(SPList list)
		{  
			return new SlamType(list);
		}

		public static SlamType Get(SPContentType contentType)
		{	
			return new SlamType(contentType);
		}

		public static SlamItem Get(SPListItem listItem)
		{	
			return new SlamItem(listItem);
		}

		private static Dictionary<string, SlamActionCollection> GetActionDictionary()
		{
			var actions = Cache.Get<Dictionary<string, SlamActionCollection>>(Constants.Cache.SlamActionCollection);
			if (actions == null)
			{
				actions = new Dictionary<string, SlamActionCollection>();
				SlamContext.Cache.Add(Constants.Cache.SlamActionCollection, actions, CacheScope.Thread);
			}
			return actions;
		}

		internal static SlamActionCollection GetTypeActionCollection(string id)
		{	
			return GetActionDictionary()[id];
		}

		internal static void RegisterTypeActionCollection(string id, SlamActionCollection actions)
		{
			if (!GetActionDictionary().ContainsKey(id))
				GetActionDictionary().Add(id, actions);
		}
	}
}
